package generatedClasses;

import java.util.*;
import struts.*;
import struts.tools.*;

public class DAOContactCreator
{
  protected static String nl;
  public static synchronized DAOContactCreator create(String lineSeparator)
  {
    nl = lineSeparator;
    DAOContactCreator result = new DAOContactCreator();
    nl = null;
    return result;
  }

  public final String NL = nl == null ? (System.getProperties().getProperty("line.separator")) : nl;
  protected final String TEXT_1 = "";
  protected final String TEXT_2 = NL;
  protected final String TEXT_3 = NL;
  protected final String TEXT_4 = NL;
  protected final String TEXT_5 = NL;
  protected final String TEXT_6 = NL;
  protected final String TEXT_7 = NL;
  protected final String TEXT_8 = NL;
  protected final String TEXT_9 = NL;
  protected final String TEXT_10 = NL;
  protected final String TEXT_11 = NL;
  protected final String TEXT_12 = NL;
  protected final String TEXT_13 = NL;
  protected final String TEXT_14 = NL;
  protected final String TEXT_15 = NL;
  protected final String TEXT_16 = NL;
  protected final String TEXT_17 = NL;
  protected final String TEXT_18 = NL;
  protected final String TEXT_19 = NL;
  protected final String TEXT_20 = NL;
  protected final String TEXT_21 = NL;
  protected final String TEXT_22 = NL;
  protected final String TEXT_23 = NL;
  protected final String TEXT_24 = NL;
  protected final String TEXT_25 = NL;
  protected final String TEXT_26 = NL;
  protected final String TEXT_27 = NL;
  protected final String TEXT_28 = NL;
  protected final String TEXT_29 = NL + NL;
  protected final String TEXT_30 = "\tfinal String ";
  protected final String TEXT_31 = " = lResultSet.getString(\"";
  protected final String TEXT_32 = "\");" + NL + "\t";
  protected final String TEXT_33 = NL + NL + "\t";
  protected final String TEXT_34 = "\tlContact.set";
  protected final String TEXT_35 = "(";
  protected final String TEXT_36 = ");" + NL + "\t";
  protected final String TEXT_37 = NL + "\t";
  protected final String TEXT_38 = NL;
  protected final String TEXT_39 = NL;
  protected final String TEXT_40 = NL;
  protected final String TEXT_41 = NL;
  protected final String TEXT_42 = "\t\t\t";
  protected final String TEXT_43 = NL;
  protected final String TEXT_44 = NL;
  protected final String TEXT_45 = NL;
  protected final String TEXT_46 = NL;
  protected final String TEXT_47 = NL;
  protected final String TEXT_48 = NL;
  protected final String TEXT_49 = NL;
  protected final String TEXT_50 = NL;
  protected final String TEXT_51 = NL;
  protected final String TEXT_52 = NL;
  protected final String TEXT_53 = NL;
  protected final String TEXT_54 = NL;
  protected final String TEXT_55 = NL;
  protected final String TEXT_56 = NL;
  protected final String TEXT_57 = "final String ";
  protected final String TEXT_58 = ",";
  protected final String TEXT_59 = "final String ";
  protected final String TEXT_60 = ")";
  protected final String TEXT_61 = NL + "\t\t\t";
  protected final String TEXT_62 = NL;
  protected final String TEXT_63 = NL;
  protected final String TEXT_64 = NL;
  protected final String TEXT_65 = NL;
  protected final String TEXT_66 = NL;
  protected final String TEXT_67 = NL;
  protected final String TEXT_68 = NL;
  protected final String TEXT_69 = ",";
  protected final String TEXT_70 = ")";
  protected final String TEXT_71 = NL + "\t\t\t";
  protected final String TEXT_72 = NL + "\t\t\t\tlPreparedStatementCreation.setString(";
  protected final String TEXT_73 = ", ";
  protected final String TEXT_74 = ");" + NL + "\t\t\t";
  protected final String TEXT_75 = "\t\t";
  protected final String TEXT_76 = NL;
  protected final String TEXT_77 = NL;
  protected final String TEXT_78 = NL;
  protected final String TEXT_79 = NL;
  protected final String TEXT_80 = NL;
  protected final String TEXT_81 = NL;
  protected final String TEXT_82 = "\t\t\t";
  protected final String TEXT_83 = NL;
  protected final String TEXT_84 = NL;
  protected final String TEXT_85 = NL;
  protected final String TEXT_86 = NL + "\t";
  protected final String TEXT_87 = NL;

  public String generate(Object argument)
  {
    final StringBuffer stringBuffer = new StringBuffer();
    stringBuffer.append(TEXT_1);
    stringBuffer.append( "package struts.domain;" );
    stringBuffer.append(TEXT_2);
    stringBuffer.append("import java.sql.Connection;");
    stringBuffer.append(TEXT_3);
    stringBuffer.append("import java.sql.PreparedStatement;");
    stringBuffer.append(TEXT_4);
    stringBuffer.append("import java.sql.ResultSet;");
    stringBuffer.append(TEXT_5);
    stringBuffer.append("import java.sql.SQLException;");
    stringBuffer.append(TEXT_6);
    stringBuffer.append("import java.sql.Statement;");
    stringBuffer.append(TEXT_7);
    stringBuffer.append("import java.util.LinkedList;");
    stringBuffer.append(TEXT_8);
    stringBuffer.append("import java.util.List;");
    stringBuffer.append(TEXT_9);
    stringBuffer.append("import javax.naming.Context;");
    stringBuffer.append(TEXT_10);
    stringBuffer.append("import javax.naming.InitialContext;");
    stringBuffer.append(TEXT_11);
    stringBuffer.append("import javax.naming.NamingException;");
    stringBuffer.append(TEXT_12);
    stringBuffer.append("import javax.sql.DataSource;");
    stringBuffer.append(TEXT_13);
     ArrayList<DataElement> formElts = (ArrayList<DataElement>) argument;
    stringBuffer.append(TEXT_14);
    stringBuffer.append(TEXT_15);
    stringBuffer.append( "public class DAOContact {");
    stringBuffer.append(TEXT_16);
    stringBuffer.append( "private final static String RESOURCE_JDBC = \"java:comp/env/jdbc/dsMyDB\";");
    stringBuffer.append(TEXT_17);
    stringBuffer.append( "public DTOListContact displayContacts() {");
    stringBuffer.append(TEXT_18);
    stringBuffer.append( "	final DTOListContact returnedContact = new DTOListContact();");
    stringBuffer.append(TEXT_19);
    stringBuffer.append( "	try {");
    stringBuffer.append(TEXT_20);
    stringBuffer.append( "		//Context from Javax naming");
    stringBuffer.append(TEXT_21);
    stringBuffer.append( "		final Context lContext = new InitialContext();");
    stringBuffer.append(TEXT_22);
    stringBuffer.append( "		final DataSource lDataSource = (DataSource) lContext.lookup(RESOURCE_JDBC);");
    stringBuffer.append(TEXT_23);
    stringBuffer.append( "		final List<Contact> contactList = new LinkedList<Contact>();");
    stringBuffer.append(TEXT_24);
    stringBuffer.append( "		final Connection lConnection  = lDataSource.getConnection();");
    stringBuffer.append(TEXT_25);
    stringBuffer.append( "		final Statement lStatement = lConnection.createStatement();");
    stringBuffer.append(TEXT_26);
    stringBuffer.append( "		final ResultSet lResultSet = lStatement.executeQuery(\"SELECT * FROM CONTACT\");");
    stringBuffer.append(TEXT_27);
    stringBuffer.append( "		while (lResultSet.next()) {");
    stringBuffer.append(TEXT_28);
    stringBuffer.append( "		final Contact lContact = new Contact();");
    stringBuffer.append(TEXT_29);
    	for(DataElement de:formElts){ 
    stringBuffer.append(TEXT_30);
    stringBuffer.append(de.getName().replaceAll("[ ]",""));
    stringBuffer.append(TEXT_31);
    stringBuffer.append(de.getName().replaceAll("[ ]","").toUpperCase());
    stringBuffer.append(TEXT_32);
    }
    stringBuffer.append(TEXT_33);
    	for(DataElement de:formElts){ 
    stringBuffer.append(TEXT_34);
    stringBuffer.append(de.getName().replaceAll("[ ]",""));
    stringBuffer.append(TEXT_35);
    stringBuffer.append(de.getName().replaceAll("[ ]",""));
    stringBuffer.append(TEXT_36);
    }
    stringBuffer.append(TEXT_37);
    stringBuffer.append(TEXT_38);
    stringBuffer.append( "		contactList.add(lContact);");
    stringBuffer.append(TEXT_39);
    stringBuffer.append("}");
    stringBuffer.append(TEXT_40);
    stringBuffer.append(TEXT_41);
    stringBuffer.append( "	returnedContact.setContactList(contactList);");
    stringBuffer.append(TEXT_42);
    stringBuffer.append(TEXT_43);
    stringBuffer.append( "	} catch (NamingException e) {");
    stringBuffer.append(TEXT_44);
    stringBuffer.append( "		// Trace errors in naming (the datasource resource in this case)");
    stringBuffer.append(TEXT_45);
    stringBuffer.append( "		e.printStackTrace();");
    stringBuffer.append(TEXT_46);
    stringBuffer.append( "		// save the error in the DTO");
    stringBuffer.append(TEXT_47);
    stringBuffer.append( "		returnedContact.setError(\"NamingException : \" + e.getMessage());");
    stringBuffer.append(TEXT_48);
    stringBuffer.append( "	} catch (SQLException e) {");
    stringBuffer.append(TEXT_49);
    stringBuffer.append( "		// Trace SQL exceptions (bad columns name, JDBC errors, SQL errors, etc.");
    stringBuffer.append(TEXT_50);
    stringBuffer.append( "		e.printStackTrace();");
    stringBuffer.append(TEXT_51);
    stringBuffer.append( "		// save the error in the DTO");
    stringBuffer.append(TEXT_52);
    stringBuffer.append( "		returnedContact.setError(\"SQLException : \" + e.getMessage());");
    stringBuffer.append(TEXT_53);
    stringBuffer.append( "	}");
    stringBuffer.append(TEXT_54);
    stringBuffer.append( "	return returnedContact;");
    stringBuffer.append(TEXT_55);
    stringBuffer.append( "}");
    stringBuffer.append(TEXT_56);
    stringBuffer.append( "public String addContact(");
    	for(int i=0;i<formElts.size()-1;i++){ 
    stringBuffer.append(TEXT_57);
    stringBuffer.append(formElts.get(i).getName().replaceAll("[ ]",""));
    stringBuffer.append(TEXT_58);
    }
    stringBuffer.append(TEXT_59);
    stringBuffer.append(formElts.get(formElts.size()-1).getName().replaceAll("[ ]",""));
    stringBuffer.append(TEXT_60);
    stringBuffer.append("{");
    stringBuffer.append(TEXT_61);
    stringBuffer.append(TEXT_62);
    stringBuffer.append( "	try {");
    stringBuffer.append(TEXT_63);
    stringBuffer.append( "			final Context lContext = new InitialContext();");
    stringBuffer.append(TEXT_64);
    stringBuffer.append( "			final DataSource lDataSource = (DataSource) lContext.lookup(RESOURCE_JDBC);");
    stringBuffer.append(TEXT_65);
    stringBuffer.append( "			final Connection lConnection  = lDataSource.getConnection();");
    stringBuffer.append(TEXT_66);
    stringBuffer.append( "			// adding a new contact ");
    stringBuffer.append(TEXT_67);
    stringBuffer.append( "			final PreparedStatement lPreparedStatementCreation = ");
    stringBuffer.append(TEXT_68);
    stringBuffer.append( "			lConnection.prepareStatement(\"INSERT INTO CONTACT(");
    for(int i=0;i<formElts.size()-1;i++){ 
    stringBuffer.append(formElts.get(i).getName().replaceAll("[ ]","").toUpperCase());
    stringBuffer.append(TEXT_69);
    }
    stringBuffer.append(formElts.get(formElts.size()-1).getName().replaceAll("[ ]","").toUpperCase());
    stringBuffer.append(TEXT_70);
    stringBuffer.append("VALUES(?, ?, ?, ?)\");");
    stringBuffer.append(TEXT_71);
    	for(int i=0;i<formElts.size();i++){ 
    stringBuffer.append(TEXT_72);
    stringBuffer.append(i+1);
    stringBuffer.append(TEXT_73);
    stringBuffer.append(formElts.get(i).getName().replaceAll("[ ]",""));
    stringBuffer.append(TEXT_74);
    }
    stringBuffer.append(TEXT_75);
    stringBuffer.append(TEXT_76);
    stringBuffer.append( "			lPreparedStatementCreation.executeUpdate();");
    stringBuffer.append(TEXT_77);
    stringBuffer.append( "			return null;");
    stringBuffer.append(TEXT_78);
    stringBuffer.append( "		} catch (NamingException e) {	e.printStackTrace();");
    stringBuffer.append(TEXT_79);
    stringBuffer.append( "		return \"NamingException : \" + e.getMessage();");
    stringBuffer.append(TEXT_80);
    stringBuffer.append( "		} catch (SQLException e) {		e.printStackTrace();");
    stringBuffer.append(TEXT_81);
    stringBuffer.append( "		return \"SQLException : \" + e.getMessage();");
    stringBuffer.append(TEXT_82);
    stringBuffer.append(TEXT_83);
    stringBuffer.append( "	}");
    stringBuffer.append(TEXT_84);
    stringBuffer.append( "	}");
    stringBuffer.append(TEXT_85);
    stringBuffer.append( "}");
    stringBuffer.append(TEXT_86);
    stringBuffer.append(TEXT_87);
    return stringBuffer.toString();
  }
}
